package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.ClubConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.MyCountDownTime;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.SwitchAccountUtils;
import com.uchengwang.ui.utils.UCommonUtil;

/**
 * Created by U城网 on 2017/8/8.
 * 转移社团管理权验证
 */

public class TransferCodeActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private TextView text;
    private EditText edit;
    private Button getCode, bt;
    private MyCountDownTime countDownTime;

    private String code;
    private long receiveUid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_transfer_code);

        receiveUid = getIntent().getLongExtra(BaseConfig.UID, 0);
        setTitle("验证");

        initView();
    }

    private void initView() {
        text = (TextView) findViewById(R.id.transfer_text);
        edit = (EditText) findViewById(R.id.transfer_edit);
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence)){
                    bt.setBackgroundResource(R.drawable.selector_fast_release);
                    bt.setTextColor(getResources().getColor(R.color.white));
                    bt.setEnabled(true);
                }else {
                    bt.setBackgroundResource(R.drawable.shape_login_no);
                    bt.setTextColor(getResources().getColor(R.color.selector_login_color));
                    bt.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        getCode = (Button) findViewById(R.id.transfer_getCode);
        getCode.setOnClickListener(this);
        bt = (Button) findViewById(R.id.transfer_bt);
        bt.setBackgroundResource(R.drawable.shape_login_no);
        bt.setEnabled(false);

        bt.setOnClickListener(this);

        initData();
//        getCode();

    }

    private void initData(){
        String phone = SPUtils.getPreferences(this).getUserName();
        if (!TextUtils.isEmpty(phone)){
            phone = phone.replace(phone.substring(3, 7), "****");
            text.setText("验证码已发送至"+phone);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.transfer_getCode://获取验证码
                getCode();
                break;
            case R.id.transfer_bt://确定
                code = edit.getText().toString();
                if (TextUtils.isEmpty(code)){
                    UCommonUtil.getUtil().showToast(this, "验证码不能为空");
                    return;
                }

                //转让社团管理权
//                Intent intent = new Intent(this, ClubMemberActivity.class);
//                intent.putExtra(BaseConfig.ORG_ID, UCommonUtil.getUtil().getId());
//                intent.putExtra(BaseConfig.TYPE, 1);
//                intent.putExtra(BaseConfig.CODE, code);
//                startActivity(intent);

                transfer();
                break;

        }
    }

    /**
     * 获取验证码
     * */
    private void getCode(){
        MapRequest map = new MapRequest();
        HttpManager.newInstance(this).query(ClubConfig.Transfer.method_post, ClubConfig.Transfer.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                if (status ==0){
                    UCommonUtil.getUtil().showToast(TransferCodeActivity.this, "发送成功");
                }
            }
        });

        if (countDownTime == null){
            countDownTime = new MyCountDownTime(60000, 1000, getCode);
        }
        countDownTime.start();
    }


    private void transfer(){
        LoadingUtils.getUtils().loading(this);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CODE, code);
        map.put(BaseConfig.RECEIVE_UID, receiveUid);
        HttpManager.newInstance(this).query(ClubConfig.Transfer.method_put, ClubConfig.Transfer.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().showToast(TransferCodeActivity.this, "转让失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "stutas="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                if (status ==0){
//                    SwitchAccountUtils.getUtils().transferClubexit(TransferCodeActivity.this);
                    SwitchAccountUtils.getUtils().toPersonAccount(TransferCodeActivity.this);
                    TransferCodeActivity.this.finish();

                }else {
                    UCommonUtil.getUtil().showToast(TransferCodeActivity.this, "转让失败");
                }
            }
        });
    }

}
